<?php

namespace addons\zzcash\controller\api;

use addons\zzcash\model\OutM;
use addons\zzcash\service\PayS;
use app\common\controller\Api;
use app\common\model\User;
use think\Validate;

class Out extends Api
{
    protected $noNeedRight = 'index';

    public function index()
    {
        $validate = new Validate();
        if (!$validate->check(input(), [
            'money' => 'require|integer'
        ])) {
            $this->error($validate->getError());
        }
        $money = input('money/d');
        if ($money < 1) {
            $this->error('money最少得1');
        }
        $user = $this->auth->getUser();
        if ($user['money'] < $money) {
            $this->error('余额不足');
        }
        User::money($money, $user['id'], '提现');
        $out    = OutM::create([
            'user_id'  => $user['id'],
            'money'    => $money,
            'payed_is' => OutM::PAYED_IS_NO,
        ]);
        $config = get_addon_config('zzcash');
        if ($config['out_auto_is'] == 'yes') {
            PayS::transfer($out);
            $this->success('转账成功');
        }
        $this->success('申请成功');
    }
}
